=head1 NAME

EPrints::Plugin::Screen::EPrint::Remove

=cut

package EPrints::Plugin::Screen::EPrint::AddToShelf;

our @ISA = ( 'EPrints::Plugin::Screen::EPrint' );

use strict;

sub new
{
	my( $class, %params ) = @_;

	my $self = $class->SUPER::new(%params);

        $self->{icon} = "action_shelf_contents.png";

	$self->{appears} = [
		{
			place => "eprint_actions",
			position => 1600,
		},
		{
			place => "eprint_item_actions",
			position => 100,
		},
		{
			place => "eprint_actions_bar_archive",
			position => 1000,
		},
                {
                        place => "eprint_summary_page_actions",
                        position => 100,
                },
	];
	
	$self->{actions} = [qw/ add /];

	return $self;
}

sub can_be_viewed
{
	my( $self ) = @_;
	
	return 1 if $self->{repository}->current_user();
}

sub render
{
	my( $self ) = @_;

	my $repo = $self->{repository};	

	my $div = $repo->make_element( "div", class=>"ep_block" );

	$div->appendChild( $self->html_phrase("choose_shelf",
		title=>$self->{processor}->{eprint}->render_description() ) );

	my %buttons = (
		add => $self->phrase(
				"action_add" ),
		_order => [ "add" ]
	);

	my $shelves = $repo->dataset("shelf")->search(filters => [
			{
				meta_fields=>[ 'userid', 'adminids', 'editorids' ],
				value=> $repo->current_user->id,
				match=>'EX',
				describe=>0,
			},
		],
		custom_order => "-lastmod",
		allow_blank => 1
	);

	print STDERR "\n\nTHE LIST HAS ".$shelves->count()." SHELVES\n\n";
	my $form= $self->render_form;

	my $xml = $repo->xml;
	my $div_select = $xml->create_element("div", class=>"antho_select_shelf");
	$form->appendChild($div_select);
	my $select = $xml->create_element("select", name=>"shelfid");
	$div_select->appendChild($select);

	$shelves->map(sub {
		my ( $repo, $dataset, $shelf, $select ) = @_;
		my $xml = $repo->xml;
		my $option = $xml->create_element("option", value => $shelf->id());
		$select->appendChild($option);
		$option->appendChild($xml->create_text_node($shelf->value("title")));
	}, $select ); 

	$form->appendChild( 
		$self->{session}->render_action_buttons( 
			%buttons ) );
	$div->appendChild( $form );

	return( $div );
}	

sub allow_add
{
	my( $self ) = @_;

	return $self->can_be_viewed;
}

sub action_add
{
	my( $self ) = @_;

	$self->{processor}->{screenid} = "EPrint::View";
	
	my $repo = $self->{repository};

	my $shelf = $repo->dataset("shelf")->dataobj($repo->param("shelfid"));

	if($shelf->has_editor( $repo->current_user ) )
	{
        	$shelf->add_items( $self->{processor}->{eprintid} );
		$self->{processor}->add_message( "message", $self->html_phrase( "item_added" ) );
		return;
	}
	
	my $db_error = $self->{session}->get_database->error;
	$self->{processor}->add_message( "message", $self->html_phrase( "item_not_added" ) );
	return;

}


1;

=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2000-2011 University of Southampton.

=for COPYRIGHT END

=for LICENSE BEGIN

This file is part of EPrints L<http://www.eprints.org/>.

EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EPrints is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public
License along with EPrints.  If not, see L<http://www.gnu.org/licenses/>.

=for LICENSE END

